package counter.com.baomidou.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import counter.com.baomidou.entity.Callbackdatainfo;
import counter.com.baomidou.mapper.CallbackdatainfoMapper;
import counter.com.baomidou.service.ICallbackdatainfoService;
import counter.com.common.Result;
import counter.com.connproperty.callback.CallbackSearchProperty;
import counter.com.connproperty.callback.CallbackSearchResponseProperty;
import counter.com.connproperty.callback.CallbackStatisticsResponseProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-09-28
 */
@RestController
@RequestMapping("/callbackdatainfo")
public class CallbackdatainfoController {
    @Autowired
    ICallbackdatainfoService callbackdatainfoService;

    @Autowired
    CallbackdatainfoMapper callbackdatainfoMapper;

    private static int SPAN_DAY = 1;
    private static int SPAN_WEEK = 2;
    private static int SPAN_MONTH = 3;
    private static int SPAN_HALF_YEAR = 4;
    private static int splitLen = 6;

    public static final int TYPE_UPLOAD = 0;
    public static final int TYPE_DOWNLOAD = 1;

    @PostMapping("/search")
    public Result search(@RequestBody CallbackSearchProperty data) {
        LocalDateTime nowTime = LocalDateTime.now();
        int spanday = data.getSpanday();
        List<LocalDateTime> requireLocalDateTimeList = getRequireLocalDateTimeList(spanday, splitLen, nowTime);
        List<List<Callbackdatainfo>> callbackdatainfos = new LinkedList<>();
        for (int i = requireLocalDateTimeList.size() - 2; i >= 0; i--) {
            LocalDateTime littleTime = requireLocalDateTimeList.get(i + 1);
            LocalDateTime bigTime = requireLocalDateTimeList.get(i);
            int type = data.getType();
            LambdaQueryWrapper<Callbackdatainfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Callbackdatainfo::getType, type).gt(Callbackdatainfo::getCreatedtime, littleTime).lt(Callbackdatainfo::getCreatedtime, bigTime);
            List<Callbackdatainfo> buffer = callbackdatainfoMapper.selectList(wrapper);
            callbackdatainfos.add(buffer);
        }
        List<LocalDateTime> timeSpans = new LinkedList<>();
        for (int i = requireLocalDateTimeList.size() - 1; i >= 0; i--) {
            timeSpans.add(requireLocalDateTimeList.get(i));
        }
        return Result.success(new CallbackSearchResponseProperty(timeSpans, callbackdatainfos));
    }

    /**
     * 将当前时间nowTime到要求时间requireTime之间的时间分成len份
     */
    private List<LocalDateTime> getRequireLocalDateTimeList(int spanday, int len, LocalDateTime nowTime) {
        List<LocalDateTime> timeList = new ArrayList<>(len);
        long minusStep = 0;
        if (spanday == SPAN_DAY) {
            minusStep = 86400 / len;
        } else if (spanday == SPAN_WEEK) {
            minusStep = 7 * 86400L / len;
        } else if (spanday == SPAN_MONTH) {
            minusStep = 31556952L / 12 / len;
        } else if (spanday == SPAN_HALF_YEAR) {
            minusStep = 31556952L / 2 / len;
        }
        //添加最初的元素
        timeList.add(nowTime);
        //添加之后的元素
        for (int i = 0; i < len; i++) {
            nowTime = nowTime.minus(minusStep, ChronoUnit.SECONDS);
            timeList.add(nowTime);
        }
        return timeList;
    }
}
